<%@ page language="java" import="com.mingrisoft.Function" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!doctype html>
<html class="no-js">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>明日科技有限公司</title>
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <link rel="stylesheet"
          href="<%=request.getContextPath()%>/statics/admin/css/amazeui.min.css">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/statics/admin/css/admin.css">
</head>
<body>
<header class="am-topbar admin-header">
    <div class="am-topbar-brand">
        <strong>明日科技</strong>
        <small>后台管理</small>
    </div>

    <button
            class="am-topbar-btn am-topbar-toggle am-btn am-btn-sm am-btn-success am-show-sm-only"
            data-am-collapse="{target: '#topbar-collapse'}">
        <span class="am-sr-only">导航切换</span>
        <span class="am-icon-bars"></span>
    </button>

    <div class="am-collapse am-topbar-collapse" id="topbar-collapse">

        <ul
                class="am-nav am-nav-pills am-topbar-nav am-topbar-right admin-header-list">

            <li class="am-dropdown" data-am-dropdown>
                <a class="am-dropdown-toggle" data-am-dropdown-toggle
                   href="javascript:;"> <span class="am-icon-users"></span>
                    <%=session.getAttribute("AdminName")%>，欢迎您 <span class="am-icon-caret-down"></span> </a>
                <ul class="am-dropdown-content">
                    <li>
                        <a href="<%=request.getContextPath()%>/front/logout"><span class="am-icon-power-off"></span>
                            退出</a>
                    </li>
                </ul>
            </li>
        </ul>
    </div>
</header>

<div class="am-cf admin-main">
    <!-- sidebar start -->
    <div class="admin-sidebar am-offcanvas" id="admin-offcanvas">
        <div class="am-offcanvas-bar admin-offcanvas-bar">
            <ul class="am-list admin-sidebar-list">
                <li>
                    <a href="<%=request.getContextPath()%>/admin/adminUser" title="新闻管理"><span
                            class="am-icon-pencil-square-o"></span> 新闻管理</a>
                </li>

                <li>
                    <a href="<%=request.getContextPath()%>/admin/adminUser" title="管理员管理"><span
                            class="am-icon-bookmark"></span> 管理员管理</a>
                </li>
            </ul>

            <div class="am-panel am-panel-default admin-sidebar-panel">
                <div class="am-panel-bd">
                    <p>
                        公告
                    </p>
                    <p>
                        技术支持：明日科技

                    </p>
                </div>
            </div>

        </div>

    </div>
    <!-- sidebar end -->

    <!-- content start -->
    <div class="admin-content">

        <div class="am-cf am-padding">
            <div class="am-fl am-cf">
                <strong>后台管理</strong>/
                <strong>新闻管理</strong>
            </div>
        </div>

        <form action="<%=request.getContextPath()%>/admin/news" method="get">
            <input name="page" value="1" hidden>
            <input name="pagesize" value="10" hidden>
            <div class="am-g" style="margin-bottom: 20px">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <label>序号:</label>
                            <input class="am-form-group-sm" name="NewsID">
                        </div>
                        <div class="am-btn-group am-btn-group-xs">
                            <label>新闻标题:</label>
                            <input class="am-form-group-sm" name="NewsContent">
                        </div>
                        <div class="am-btn-group am-btn-group-xs">
                            <button type="submit" class="am-btn am-btn-success">搜索</button>
                        </div>
                        <div class="am-btn-group am-btn-group-xs">
                            <button type="button" class="am-btn am-btn-success"
                                    data-am-modal="{target: '#new-popup'}">
                                <span class="am-icon-plus"></span> 添加新闻
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </form>

        <div class="am-g" style="height: 300px">
            <div class="am-u-sm-12">
                <form class="am-form">
                    <table
                            class="am-table am-table-striped am-table-hover table-main">
                        <thead>
                        <tr>
                            <th class="table-id">
                                序号
                            </th>
                            <th class="table-title">
                                新闻标题
                            </th>
                            <th class="table-title">
                                新闻内容
                            </th>
                            <th class="table-title">
                                创建人
                            </th>
                            <th class="table-author ">
                                创建时间
                            </th>

                            <th class="table-author ">
                                操作
                            </th>
                        </tr>
                        </thead>
                        <tbody id="itemList">
                            <c:choose>
                                <c:when test="${not empty data}">
                                    <c:forEach items="${data.news}" var="item">
                                        <tr>
                                            <td class="table-id">${item.NewsID}</td>
                                            <td >${item.NewsTitle}</td>
                                            <td >${item.NewsContent}</td>
                                            <td class="table-title">${item.AdminName}</td>
                                            <td class="table-title">${item.NewsTime}</td>
                                            <td>
                                                <div class="am-btn-toolbar">
                                                    <div class="am-btn-group am-btn-group-xs">
                                                        <a href="javascript:void(0)" class="am-btn am-btn-primary am-btn-xs a-edit"
                                                           data-NewsID="${item.NewsID}" data-newsTitle="${item.NewsTitle}" data-NewsContent="${item.NewsContent}"><span></span>修改</a>
                                                        <a href="javascript:void(0)" class="am-btn am-btn-primary am-btn-xs" onclick="del(this)" data-newsID="${item.NewsID}"><span></span>删除</a>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                </c:when>
                                <c:otherwise>
                                    <tr align="center"><td colspan="6">出错啦,请重试!</td></tr>
                                </c:otherwise>
                            </c:choose>
                        </tbody>
                    </table>
                    <%-- 分页 --%>
                    <c:choose>
                        <c:when test="${not empty data}">
                            <table class="am-table am-table-striped" style="width: 90%;border: 0;" align="center" cellpadding="2" cellspacing="0">
                                <tr>
                                    <td width="80%" height="30px" class="chinese">
                                        <span class="chinese">当前第${data.page}页/共${data.totalpage}页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共${data.count}条记录&nbsp;&nbsp;&nbsp;&nbsp;${data.pagesize}条一页</span>
                                    </td>
                                    <td width="20%">
                                        <table width="100%" border="0">
                                            <tr>
                                                <td>
                                                    <div align="right">
                                                <span class="chinese">
                                                    <select id="ipage" name="ipage" class="chinese" onchange="jumpMenu('self',this,0)">
                                                        <option value="">请选择</option>
                                                    </select>
                                                </span>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </c:when>
                    </c:choose>
                </form>
            </div>

        </div>
    </div>
    <!-- content end -->

</div>


<div class="am-popup" id="new-popup">
    <div class="am-popup-inner">
        <div class="am-popup-hd">
            <h4 class="am-popup-title">
                添加新闻
            </h4>
            <span data-am-modal-close class="am-close">&times;</span>
        </div>

        <div class="am-popup-bd">
            <form action="<%=request.getContextPath()%>/admin/news" method="post"
                  class="am-form" id="new-msg">
                <fieldset>
                    <div class="am-form-group">
                        <label >
                            新闻标题：
                        </label>
                        <input name="NewsTitle" type="text" maxlength="32"
                               placeholder="请输入新闻标题" data-validation-message="不能为空" required/>
                    </div>
                    <div class="am-form-group">
                        <label >
                            新闻内容：
                        </label>
                        <textarea name="NewsContent" cols="30" rows="10"
                                  placeholder="请输入新闻内容。段落间请用#分隔。" data-validation-message="不能为空" required></textarea>
                    </div>

                    <input name="type" type="hidden" value="add">

                    <button class="am-btn am-btn-secondary" type="submit">
                        提交
                    </button>
                    <button onclick='javascript:$("#new-popup").modal("close");'
                            class="am-btn am-btn-secondary" type="button">
                        关闭
                    </button>
                </fieldset>
            </form>
        </div>

    </div>
</div>

<div class="am-popup" id="edit-popup">
    <div class="am-popup-inner">
        <div class="am-popup-hd">
            <h4 class="am-popup-title">
                修改新闻
            </h4>
            <span data-am-modal-close class="am-close">&times;</span>
        </div>

        <div class="am-popup-bd">
            <form action="<%=request.getContextPath()%>/admin/news" method="post"
                  class="am-form" id="edit-msg">
                <fieldset>
                    <div class="am-form-group">
                        <label >
                            序号：
                        </label>
                        <input id="upd_newsId" name="upd_newsId" type="text" maxlength="32" readonly/>
                    </div>
                    <div class="am-form-group">
                        <label for="upd_NewsTitle">
                            新闻标题：
                        </label>
                        <input id="upd_NewsTitle" name="upd_NewsTitle" type="text" maxlength="32"
                               placeholder="请输入新闻标题" data-validation-message="不能为空" required/>
                    </div>
                    <div class="am-form-group">
                        <label for="upd_NewsContent">
                            新闻内容：
                        </label>
                        <textarea id="upd_NewsContent" name="upd_NewsContent" cols="30" rows="10"
                                  placeholder="请输入新闻内容" data-validation-message="不能为空" required></textarea>
                    </div>

                    <input name="type" type="hidden" value="edit">

                    <button class="am-btn am-btn-secondary" type="submit">
                        提交
                    </button>
                    <button onclick='javascript:$("#edit-popup").modal("close");'
                            class="am-btn am-btn-secondary" type="button">
                        关闭
                    </button>
                </fieldset>
            </form>
        </div>

    </div>
</div>


<a class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu"
   data-am-offcanvas="{target: '#admin-offcanvas'}"></a>

<div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm">
    <div class="am-modal-dialog">
        <div class="am-modal-bd">
            确定要删除当前主题吗？
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn" data-am-modal-cancel>取消</span>
            <span class="am-modal-btn" data-am-modal-confirm>确定</span>
        </div>
    </div>
</div>
<footer>
    <hr>
    <p class="am-padding-left">
        © 2018 明日科技
    </p>
</footer>

<script src="<%=request.getContextPath()%>/statics/admin/js/jquery.min.js">
</script>
<script src="<%=request.getContextPath()%>/statics/admin/js/amazeui.min.js">
</script>
<script>

    function jumpMenu(s1, s2, s3) {
        var pageURL = $('#ipage option:selected').val();
        window.location.href = pageURL;
    }

    var base = "<%=request.getContextPath()%>"
</script>
<script>
    $(function () {

        $('#new-msg').validator(
            {
                onValid: function (validity) {
                    $(validity.field).closest('.am-form-group').find(
                        '.am-alert').hide();
                },

                onInValid: function (validity) {
                    var $field = $(validity.field);
                    var $group = $field.closest('.am-form-group');
                    var $alert = $group.find('.am-alert');

                    var msg = $field.data('validationMessage')
                        || this.getValidationMessage(validity);

                    if (!$alert.length) {
                        $alert = $('<div class="am-alert am-alert-danger"></div>')
                            .hide().appendTo($group);
                    }

                    $alert.html(msg).show();
                }
            });


        $('#edit-msg').validator(
            {
                onValid: function (validity) {
                    $(validity.field).closest('.am-form-group').find(
                        '.am-alert').hide();
                },

                onInValid: function (validity) {
                    var $field = $(validity.field);
                    var $group = $field.closest('.am-form-group');
                    var $alert = $group.find('.am-alert');

                    var msg = $field.data('validationMessage')
                        || this.getValidationMessage(validity);

                    if (!$alert.length) {
                        $alert = $('<div class="am-alert am-alert-danger"></div>')
                            .hide().appendTo($group);
                    }

                    $alert.html(msg).show();
                }
            });

    });

    //删除
    function del(obj) {
        $('#my-confirm').modal({
            relatedTarget: this,
            onConfirm: function (options) {
                var delId = $(obj).attr("data-newsID");
                $.ajax({
                    url: base + "/admin/news",
                    method: 'post',
                    data: {
                        type: "del",
                        delId: delId
                    },
                    dataType: 'json',
                    success: function (res) {
                        if(res.errcode == 0){
                            alert("删除成功")
                            window.location.reload()
                        }else {
                            alert("删除失败")
                        }
                    },
                    error: function (err) {
                        alert("删除失败")
                    }
                })
            },
            onCancel: function () {
            }
        });
    }



    //监听编辑事件
    $("#itemList").on("click",".a-edit",function () {
        //取值
        var newsTitle = $(this).attr("data-newsTitle")
        var NewsContent = $(this).attr("data-NewsContent")
        var NewsId = $(this).attr("data-NewsID")

        $('#upd_NewsTitle').val(newsTitle);
        $('#upd_NewsContent').val(NewsContent);
        $('#upd_newsId').val(NewsId);
        $('#edit-popup').modal();
    })
</script>
</body>
</html>
